package com.secondary.action.userAction;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.secondary.action.BaseAction;
import com.secondary.pojos.Category;
import com.secondary.pojos.Goods;
import com.secondary.pojos.User;
import com.secondary.service.CategoryService;
import com.secondary.service.GoodsService;
import com.secondary.service.UserService;
import com.secondary.util.MyImageUtil;

@ParentPackage(value = "struts-default")
@Namespace(value = "/user")
@Action(value = "user")
@Results( {
		@Result(name = "gotoRegister", location = "/user/register.jsp"),
		@Result(name = "regSuccess", location = "/main/showIndex!show.action", type = "redirect"),
		@Result(name = "logSuccess", location = "/main/showIndex!show.action", type = "redirect"),
		@Result(name = "gotoLogin", location = "/user/login.jsp"),
		@Result(name = "logFail", location = "/user/login.jsp"),		
		@Result(name = "stateError", location = "/user/login.jsp") })
		
		
public class UserAction extends BaseAction {
	@Resource
	private UserService userService;
	@Resource
	private CategoryService categoryService;
	@Resource
	private GoodsService goodsService;
	private User user;
	
	private Goods goods;
	private String logMessage;

	private File image;
	private String imageFileName;
	private String imageContentType;

	public String register() {
		System.out.println(user);
		Date regDate = new Date(System.currentTimeMillis());
		user.setRegeistDate(regDate);
		user.setLastDate(regDate);
		String ip = super.getRequest().getRemoteAddr();
		user.setLastIp(ip);
		userService.saveUser(user);
		System.out.println("\\\\\\" + user.getId());
		super.setSessionAttribute("user", user);
		return "regSuccess";
	}

	public String gotoRegister() {
		return "gotoRegister";
	}

	public String login() {
		String username = super.getRequestParameter("username");
		String pwd = super.getRequestParameter("pwd");
		User lgUser = userService.login(username, pwd);
		if (lgUser != null) {
			String ip = super.getRequest().getRemoteAddr();
			if (lgUser.getState() == 1) {
				logMessage = "您的账号已被封停，请联系管理员";
				return "stateError";
			}
			super.setSessionAttribute("user", lgUser);
			userService.updateUserLogInfo(lgUser, ip);
			return "logSuccess";
		} else {
			logMessage = "账号或密码错误，请重新输入";
			return "logFail";
		}

	}
	public String checkUserAccount(){
		String userAccount=super.getRequestParameter("userAccount");
		boolean flag=userService.findUserAccount(userAccount);
		JSONObject jsonObject = new JSONObject();
		if(flag){
			jsonObject.accumulate("flag", "success");
			super.renderText(jsonObject.toString());
		}else{
			jsonObject.accumulate("flag", "fail");
			super.renderText(jsonObject.toString());
		}
		return null;
	}
	public String checkCode(){
		String code=(String)super.getSessionAttribute("sessionCode");
		String reqCode=super.getRequestParameter("code");
		JSONObject jsonObject = new JSONObject();
		if(code.equals(reqCode)){
			jsonObject.accumulate("flag", "success");
			super.renderText(jsonObject.toString());
		}else{
			jsonObject.accumulate("flag", "fail");
			super.renderText(jsonObject.toString());
		}
		return null;
	}

	
	public String gotoLogin() {
		return "gotoLogin";
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}


	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}

	public String getLogMessage() {
		return logMessage;
	}

	public void setLogMessage(String logMessage) {
		this.logMessage = logMessage;
	}

}
